package com.finger.gulimall.product.app;

import java.util.Arrays;
import java.util.Map;
import java.util.UUID;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.finger.common.to.cart.CartItemTo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.finger.gulimall.product.entity.SkuInfoEntity;
import com.finger.gulimall.product.service.SkuInfoService;
import com.finger.common.utils.PageUtils;
import com.finger.common.utils.R;


/**
 * sku信息
 *
 * @author finger
 * @email 909128695@qq.com
 * @date 2022-03-16 12:43:42
 */
@RestController
@RequestMapping("product/skuinfo")
public class SkuInfoController {

    @Autowired
    private SkuInfoService skuInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("product:skuinfo:list")
    public R list(@RequestParam Map<String, Object> params) {
        // 条件分页查询
        PageUtils page = skuInfoService.queryPageCondition(params);

        return R.ok().put("page", page);
    }

    @GetMapping("/getSkuName")
//    @RequiresPermissions("product:skuinfo:list")
    public R getSkuName(@RequestParam("skuId") Long skuId) {
        SkuInfoEntity skuInfoEntity = skuInfoService.getById(skuId);
        return R.ok().put("skuName", skuInfoEntity.getSkuName());
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{skuId}")
    //@RequiresPermissions("product:skuinfo:info")
    public R info(@PathVariable("skuId") Long skuId) {
        SkuInfoEntity skuInfo = skuInfoService.getById(skuId);
        return R.ok().put("skuInfo",skuInfo);
    }

    /**
     * 用于购物车模块调用，封装商品上传到购物车的信息
     * @param skuId 商品id
     * @return
     */
    @RequestMapping("/cart/info/{skuId}")
    public R cartInfo(@PathVariable("skuId") Long skuId) {
        QueryWrapper<SkuInfoEntity> queryWrapper = new QueryWrapper<SkuInfoEntity>().eq("sku_id", skuId).select("sku_default_img,sku_title,price");
        SkuInfoEntity one = skuInfoService.getOne(queryWrapper);
        CartItemTo cartItemTo = new CartItemTo();
        BeanUtils.copyProperties(one,cartItemTo);
        System.out.println(cartItemTo);
        return R.ok().setData(cartItemTo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:skuinfo:save")
    public R save(@RequestBody SkuInfoEntity skuInfo) {
        skuInfoService.save(skuInfo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:skuinfo:update")
    public R update(@RequestBody SkuInfoEntity skuInfo) {
        skuInfoService.updateById(skuInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:skuinfo:delete")
    public R delete(@RequestBody Long[] skuIds) {
        skuInfoService.removeByIds(Arrays.asList(skuIds));

        return R.ok();
    }

}
